<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


    <?php
// <editor-fold defaultstate="collapsed" desc="Inclusion">
    try {
        //require('../../Smarty/libs/Smarty.class.php');
        require('../../Smarty/libs/SmartyBC.class.php');
        include_once 'API/APIObject.php';
        include_once 'API/Code/Filter.php';
        include_once 'API/Code/RoomGroup.php';
        include_once 'API/Utils.php';
    } catch (Exception $exc) {
        echo $exc->getTraceAsString();
    }// </editor-fold>
//// <editor-fold defaultstate="collapsed" desc="Session Start">
    if(!isset($_SESSION)) session_start(); // </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Smarty Variables">
    $smarty = new Smarty;
    $smarty->setTemplateDir('./smarty/templates/');
    $smarty->setCompileDir('./smarty/templates_c/');
    $smarty->setConfigDir('./smarty/configs/');
    $smarty->setCacheDir('./smarty/cache/');

    if (empty($_SESSION['logged_bool'])) {
        $_SESSION['logged_bool'] = false;
    }
    $status = $_SESSION['logged_bool'];

    $smarty->assign("logged", $status);
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Scope Variable">
// Field Variables
    $errorUrl = './errorPage.php';
    $continue = true;
    $roomCount = 0;
    $rooms = array();

// Hardcoded Variables
    $numberOfResults = 5;
    $countryCode = NULL; //$_POST["country-code"];
    $stateProvince = NULL;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="POST Variables">
    if (!empty($_POST['destination'])) {
        $destination = urlencode($_POST["destination"]);
    } else {
        $continue = false;
    }

    if (!empty($_POST["check-in"]) && Utils::is_date($_POST["check-in"]) && $continue) {
        $checkIn = $_POST["check-in"];
    } else {
        $continue = false;
    }

    if (!empty($_POST["check-out"]) && Utils::is_date($_POST["check-out"]) && $continue) {
        $checkOut = $_POST["check-out"];
    } else {
        $continue = false;
    }
    if (!empty($_POST['room_noumber']) && $continue) {
        $roomCount = $_POST['room_noumber'];
    } else {
        $continue = false;
    }

    for ($i = 1; $i <= $roomCount; $i++) {
        $rooms[$i] = $_POST["room$i"];
        for ($j = $rooms[$i]["childrenCount"] + 1; $j <= 3; $j++) {
            $rooms[$i]["children$j"] = "";
        }
    }
// </editor-fold>


    if ($continue) {
        // <editor-fold defaultstate="collapsed" desc="API USSAGE">
        $options = "DEFAULT";
        $optimizationState = NULL;

        $api = new APIObject();
        $filter = new Filter();

        $filter->supplierType = "E";
        $filter->RoomGroup = new RoomGroup($rooms, $roomCount);
        $hotelRes = $api->GetHotelList_City($checkIn, $checkOut, $numberOfResults, $destination, $stateProvince, $countryCode, $filter, $options, $optimizationState); // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Smarty Template Variable Section">
        $smartyData = $smarty->createData();
        $hotelArray = array(); // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Hotel Summary">
        for ($index = 0; $index < count($hotelRes->HotelList->HotelSummary); $index++) {
            // <editor-fold defaultstate="collapsed" desc="General Hotel Details">
            $hotelArray[$index]["thumbnailUrl"] = "http://images.travelnow.com" . $hotelRes->HotelList->HotelSummary[$index]->thumbNailUrl;
            $hotelArray[$index]["hotelName"] = $hotelRes->HotelList->HotelSummary[$index]->name;
            $hotelArray[$index]["shortDesc"] = $hotelRes->HotelList->HotelSummary[$index]->shortDescription;
            $hotelArray[$index]["rating"] = $hotelRes->HotelList->HotelSummary[$index]->hotelRating;
            $hotelArray[$index]["supplierType"] = $hotelRes->HotelList->HotelSummary[$index]->supplierType;
            $hotelArray[$index]["lRate"] = $hotelRes->HotelList->HotelSummary[$index]->lowRate;
            $hotelArray[$index]["hRate"] = $hotelRes->HotelList->HotelSummary[$index]->highRate;
            $hotelArray[$index]["rateCurrencyCode"] = $hotelRes->HotelList->HotelSummary[$index]->rateCurrencyCode;

            // Get hotelId
            $hotelId = $hotelRes->HotelList->HotelSummary[$index]->hotelId;
            $hotelArray[$index]["hotelId"] = $hotelId;

            //$hotelInfo = $api->GetHotelInformation($hotelId, "DEFAULT");
            // Get Room Availability
            $roomAvail = $api->GetRoomAvailability($hotelId, $checkIn, $checkOut, NULL, $hotelRes->HotelList->HotelSummary[$index]->supplierType, $hotelRes->HotelList->HotelSummary[$index]->
                    RoomRateDetailsList->RoomRateDetails->rateKey,
                    null,
                    null, TRUE, TRUE, "HOTEL_DETAILS,ROOM_TYPES,HOTEL_IMAGES", $filter->RoomGroup
            );
            // Get hotel information

            $hotelArray[$index]["propertyDetails"] = html_entity_decode($roomAvail->HotelDetails->propertyDescription);
            $hotelArray[$index]["id"] = $index;
            $hotelArray[$index]["hotelPolicy"] = $roomAvail->HotelDetails->hotelPolicy;
            $hotelArray[$index]["checkInInstruction"] = $roomAvail->checkInInstructions;
            $roomTypes = $roomAvail->HotelRoomResponse; // </editor-fold>
            // <editor-fold defaultstate="collapsed" desc="Room Data">
            for ($index2 = 0; $index2 < 3/* count($roomTypes) */; $index2++) {
                if (!empty($roomAvail->HotelRoomResponse[$index2]->RoomType->{"@roomCode"})) {
                    $roomAvailcancel = $api->GetRoomAvailability($hotelId, $checkIn, $checkOut, NULL, $hotelRes->HotelList->HotelSummary[$index]->supplierType, $hotelRes->HotelList->HotelSummary[$index]->
                            RoomRateDetailsList->RoomRateDetails->rateKey, $roomAvail->HotelRoomResponse[$index2]->rateCode, $roomAvail->HotelRoomResponse[$index2]->RoomType->{"@roomCode"}, TRUE, TRUE, "ROOM_TYPES", $filter->RoomGroup
                    );
                    //echo $roomAvail->HotelRoomResponse[$index2]->RoomType->{"@roomCode"};
                    if (!empty($roomAvailcancel->HotelRoomResponse->cancellationPolicy)) {
                        $hotelArray[$index]["roomTypes"][$index2]["cancelPolicy"] = $roomAvailcancel->HotelRoomResponse->cancellationPolicy;
                        $hotelArray[$index]["roomTypes"][$index2]["description"] = $roomTypes[$index2]->RoomType->description;
                        $hotelArray[$index]["roomTypes"][$index2]["descriptionLong"] = $roomTypes[$index2]->RoomType->descriptionLong;
                        $hotelArray[$index]["roomTypes"][$index2]["nonRefundable"] = $roomTypes[$index2]->nonRefundable;
                        $hotelArray[$index]["roomTypes"][$index2]["total"] = $roomAvail->HotelRoomResponse[$index2]->RateInfos->RateInfo->ChargeableRateInfo->{"@total"};
                        $hotelArray[$index]["roomTypes"][$index2]["averageRate"] = round($roomAvail->HotelRoomResponse[$index2]->RateInfos->RateInfo->ChargeableRateInfo->{"@averageRate"},2);
                    } else {
                        $hotelArray[$index]["roomTypes"][$index2]["description"] = "";
                        $hotelArray[$index]["roomTypes"][$index2]["cancelPolicy"] = "";
                        $hotelArray[$index]["roomTypes"][$index2]["descriptionLong"] = "";
                        $hotelArray[$index]["roomTypes"][$index2]["nonRefundable"] = "";
                        $hotelArray[$index]["roomTypes"][$index2]["total"] = "";
                        $hotelArray[$index]["roomTypes"][$index2]["averageRate"] = "";
                    }
                } else {
                    $hotelArray[$index]["roomTypes"][$index2]["description"] = "";
                    $hotelArray[$index]["roomTypes"][$index2]["cancelPolicy"] = "";
                    $hotelArray[$index]["roomTypes"][$index2]["descriptionLong"] = "";
                    $hotelArray[$index]["roomTypes"][$index2]["nonRefundable"] = "";
                    $hotelArray[$index]["roomTypes"][$index2]["total"] = "";
                    $hotelArray[$index]["roomTypes"][$index2]["averageRate"] = "";
                }

                //$hotelArray[$index]["roomTypes"][$index2]["chargeableRate"] = $roomAvail->HotelRoomResponse->RateInfos->RateInfo->ChargeableRateInfo->NightlyRatesPerRoom->NightlyRate[$index2]->{"@baseRate"};
                //$hotelArray[$index]["roomTypes"][$index2]["taxesAndFees"] = $roomAvail->HotelRoomResponse->RateInfos->RateInfo->ChargeableRateInfo->{"@surchargeTotal"};
                // booking stuff
                $hotelArray[$index]["supplierType"] = $hotelRes->HotelList->HotelSummary[$index]->supplierType;
                $hotelArray[$index]["roomRateKey"] = $hotelRes->HotelList->HotelSummary[$index]->RoomRateDetailsList->RoomRateDetails->rateKey;
                $hotelArray[$index]["roomRateCode"] = $hotelRes->HotelList->HotelSummary[$index]->RoomRateDetailsList->RoomRateDetails->rateCode;
                $hotelArray[$index]["roomTypeCode"] = $hotelRes->HotelList->HotelSummary[$index]->RoomRateDetailsList->RoomRateDetails->roomTypeCode;
                $hotelArray[$index]["roomTypes"][$index2]["roomId"] = $index2;
            }// </editor-fold>
            // <editor-fold defaultstate="collapsed" desc="Hotel Images">
            $hotelImages = $roomAvail->HotelImages->HotelImage;
            $hotelArray[$index]["nrOfPictures"] = count($hotelImages);

            for ($index2 = 0; $index2 < count($hotelImages); $index2++) {
                $hotelArray[$index]["hotelImages"][$index2]["thumbUrl"] =
                        $hotelImages[$index2]->thumbnailUrl;
            }
            for ($index2 = 0; $index2 < count($hotelImages); $index2++) {
                $hotelArray[$index]["hotelImages"][$index2]["url"] = $hotelImages[$index2]->url;
                $hotelArray[$index]["hotelImages"][$index2]["nr_picture"] = $index2;
            }// </editor-fold>
        }// </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Smarty Template Assignment Section">
        $smartyData->assign("hotelArray", $hotelArray);
        $smarty->display('preHeader.tpl');
        echo '<body style="top: -16px; position: relative;">';
        $smarty->display('header.tpl');
        $smarty->display('flashyBox.tpl');
        $smarty->display("hotelsList.tpl", $smartyData);
        $smarty->display('footer.tpl');
        echo '</body>';
// </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Session Section">
        $_SESSION["roomsNumber"] = $roomCount;
        for ($index = 1; $index <= $roomCount; $index++) {
            $_SESSION["roomsData"][$index]["adults"] = $rooms[$index]["adultsCount"];
            $_SESSION["roomsData"][$index]["childrenNr"] = $rooms[$index]["childrenCount"];
            $_SESSION["roomsData"][$index]["childrenAge"] =
                    $rooms[$index]["children1"] . "," .
                    $rooms[$index]["children2"] . "," .
                    $rooms[$index]["children3"];
        }
        $_SESSION["checkIn"] = $checkIn;
        $_SESSION["checkOut"] = $checkOut;
        $_SESSION["roomGroup"] = serialize($filter->RoomGroup);
    } else {
        header("Location: $errorUrl");
    }
    ?>
</html>
